//
//  JSTipsView.m
//  NiubilityProject
//
//  Created by Apple on 2017/11/23.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "JSTipsView.h"

@interface JSTipsView ()



@property(nonatomic, strong) QMUIPieProgressView *progressView;
@property(nonatomic, assign) BOOL needsHiden;
@property(nonatomic, assign) BOOL changeContent;


@end

@implementation JSTipsView

- (void)showWithProgress:(CGFloat)progress animation:(BOOL)animation{
    [self showWithProgressWithText:nil detailText:nil hideAfterDelay:0 rogress:progress animation:animation];
}

- (void)showWithProgress:(CGFloat)progress{
    [self showWithProgressWithText:nil detailText:nil hideAfterDelay:0 rogress:progress animation:NO];
}
- (void)showWithProgressWithText:(NSString *)text detailText:(NSString *)detailText hideAfterDelay:(NSTimeInterval)delay rogress:(float)progress animation:(BOOL)animation{
    if (progress < 0 )  progress = 0;
    if (progress > 1)   progress = 1;
    QMUIPieProgressView  *progressView = [[QMUIPieProgressView alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];
    [progressView addTarget:self action:@selector(handleProgressViewValueChanged:) forControlEvents:UIControlEventValueChanged];
    progressView.tintColor = [QDThemeManager sharedInstance].currentTheme.themeTintColor;
    _contentCustomView = progressView;
    [progressView setProgress:progress animated:animation];
    [self showTipWithText:text detailText:detailText hideAfterDelay:delay];
    
}
- (void)showTipWithText:(NSString *)text detailText:(NSString *)detailText hideAfterDelay:(NSTimeInterval)delay {
    
    QMUIToastContentView *contentView = (QMUIToastContentView *)self.contentView;
    contentView.customView = self.contentCustomView;
    contentView.textLabelText = text ?: @"";
    contentView.detailTextLabelText = detailText ?: @"";
    [self showAnimated:YES];
    if (delay > 0) {
        [self hideAnimated:YES afterDelay:delay];
    }
}

- (void)resetProgressTintColor:(UIColor *)color{
    if ([self.contentCustomView isKindOfClass:[QMUIPieProgressView class]]) {
        QMUIPieProgressView  *progressView = (QMUIPieProgressView *)self.contentCustomView;
        progressView.tintColor = color;
    }
}
- (void)resetProgressValue:(CGFloat)value isAnimation:(BOOL)animation isFullHiden:(BOOL)hiden{
    if (value < 0) value = 0;
    if (value > 1) value = 1;
    self.changeContent = !hiden;
    /// 注意此时是否显示在界面上
    JSLog(@"是否显示在界面上 %zd",[self.contentCustomView isShowingOnKeyWindow]);
    if ([self.contentCustomView isKindOfClass:[QMUIPieProgressView class]]) {
        QMUIPieProgressView  *progressView = (QMUIPieProgressView *)self.contentCustomView;
        [progressView setProgress:value animated:animation];
    }
    if (hiden && value == 1) {
        [self performSelector:@selector(hideAnimated:) withObject:@YES afterDelay:1];
//        [self hideAnimated:YES afterDelay:2];
    }
}

- (void)resetProgressValue:(CGFloat)value isAnimation:(BOOL)animation isFullAfterChangeContenView:(UIView *)contenView text:(NSString *)text detailText:(NSString *)detailText hideAfterDelay:(NSTimeInterval)delay{
    [self resetProgressValue:value isAnimation:animation isFullHiden:NO];
    self.changeContent = YES;
    self.contentCustomView = contenView;
    self.contentCustomText = text;
    self.contentCustomDetailsText = detailText;
    if (delay > 0) {
        [self hideAnimated:YES afterDelay:delay];
    }
}
- (void)handleProgressViewValueChanged:(QMUIPieProgressView *)progressView {
    if (progressView.progress >= 1.0 && self.changeContent) {
        [self performSelector:@selector(changeContent) withObject:nil afterDelay:1];
    }
}
-(void)changeConten{
    CATransition *anima = [CATransition animation];
    anima.type = @"fade";//设置动画的类型
    anima.subtype = kCATransitionFromRight; //设置动画的方向
    anima.duration = 0.3f;
    [self.layer addAnimation:anima forKey:@"revealAnimation"];
    QMUIToastContentView *contentView = (QMUIToastContentView *)self.contentView;
    contentView.customView = self.changeToCustomView;
    contentView.textLabelText = self.contentCustomText ?: @"";
    contentView.detailTextLabelText = self.contentCustomDetailsText ?: @"";
    [contentView setNeedsLayout];
}

@end
